{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入必要的工具包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method NDFrame.head of           id  feat_1  feat_2  feat_3  feat_4  feat_5  feat_6  feat_7  feat_8  \\\n",
       "44641  44642       0       1       0       4       0       0       0       0   \n",
       "34885  34886       0       0       0       0       0       0       0       0   \n",
       "44806  44807       0       0       8       7       0       0       0       0   \n",
       "19016  19017       0       0       0       0       0       0       0       0   \n",
       "25875  25876       0       0       0       0       0       0       0       2   \n",
       "21530  21531       0       0       0       0       0       0       0       0   \n",
       "24732  24733       0       0       0       0       0       0       1       0   \n",
       "42111  42112       0       2       0       0       0       0       0       1   \n",
       "28761  28762       0       0       0       0       0       0       0       1   \n",
       "50497  50498       0       0       0       0       0       0       0       0   \n",
       "37657  37658       0       0       0       2       0       0       0       0   \n",
       "2638    2639       0       0       0       0       0       0       0       0   \n",
       "29046  29047       0       0       0       0       0       0       0       0   \n",
       "55121  55122       0       1       0       0       0       0       3       1   \n",
       "16629  16630       0       0       0       0       0       0       0       0   \n",
       "50562  50563       0       0       0       0       0       0       1       1   \n",
       "24556  24557       0       0       0       0       0       0       0       0   \n",
       "60774  60775       0       0       0       0       0       0       0       0   \n",
       "10056  10057       1       0       0       1       0       0       0       1   \n",
       "20861  20862       0       0       0       0       0       0       0       0   \n",
       "16188  16189       0       0       0       0       0       0       0       0   \n",
       "47867  47868       0       0       0       0       0       0       0       0   \n",
       "13832  13833       0       0       0       0       0       0       0       0   \n",
       "2623    2624       0       0       0       0       0       0       0       0   \n",
       "55871  55872       0       4      40      26       0       2       0       1   \n",
       "34959  34960       0       0       1       1       0       0       0       0   \n",
       "37206  37207       0       0       1       1       0       0       0       0   \n",
       "2375    2376       0       0       0       0       0       0       0       0   \n",
       "35490  35491       0       0       0       0       0       0       0       1   \n",
       "17761  17762       0       0       0       0       0       0       0       0   \n",
       "...      ...     ...     ...     ...     ...     ...     ...     ...     ...   \n",
       "21039  21040       0       0       0       0       0       0       0       3   \n",
       "34539  34540       2       0       0       0       1       0       0       2   \n",
       "24041  24042       0       0       0       0       0       0       0       0   \n",
       "19910  19911       0       0       0       0       0       0       0       0   \n",
       "47220  47221       0       0       1       0       0       0       1       3   \n",
       "28765  28766       0       0       0       0       0       0       0       0   \n",
       "50125  50126       0       0       0       0       0       0       0       0   \n",
       "17812  17813       0       0       0       0       0       0       0       0   \n",
       "1251    1252       0       0       0       0       0       0       0       2   \n",
       "2899    2900       0       0       0       0       0       0       0       0   \n",
       "6456    6457       0       0       0       0       0       0       0       0   \n",
       "49653  49654       0       0       0       0       0       0       0       0   \n",
       "17695  17696       0       0       0       1       0       0       0       0   \n",
       "3890    3891       3       0       0       0       0       0       0       0   \n",
       "21318  21319       1       0       0       0       0       0       0       0   \n",
       "33220  33221       0       0       1       6       0       0       0       0   \n",
       "58102  58103       3       0       0       0       0       0       0       0   \n",
       "9941    9942       0       0       0       0       0       0       0       0   \n",
       "27692  27693       0       0       0       0       1       0       0       0   \n",
       "8895    8896       0       0       0       0       0       0       0       0   \n",
       "10411  10412       0       0       0       0       0       0       0       0   \n",
       "12022  12023       0       0       0       0       0       0       0       0   \n",
       "48848  48849       0       0       0       0       0       0       0       1   \n",
       "33441  33442       0       0      11       2       0       0       0       0   \n",
       "13956  13957       0       0       0       0       0       0       0       0   \n",
       "10272  10273       0       0       0       0       0       0       0       4   \n",
       "56601  56602       0       0       7      44       0       0       0       0   \n",
       "44426  44427       0       1       0       1       0       0       0       0   \n",
       "17440  17441       1       0       0       0       0       0       0       0   \n",
       "1942    1943       1       0       0       0       0       0       0       0   \n",
       "\n",
       "       feat_9  ...  feat_85  feat_86  feat_87  feat_88  feat_89  feat_90  \\\n",
       "44641       0  ...        0        0        0        0        0        0   \n",
       "34885       2  ...        0        0        0        0        0        0   \n",
       "44806       0  ...        0        0        0        0        0        0   \n",
       "19016       0  ...        0        8        0       10        0        0   \n",
       "25875       0  ...        0        1        0        0        1        0   \n",
       "21530       0  ...        0        3        0        6        0        0   \n",
       "24732       0  ...        0        1        0        0        0        0   \n",
       "42111       0  ...        0        0        0        0        1        0   \n",
       "28761       0  ...        0        0        0        0        0        0   \n",
       "50497       0  ...        0        2        0        0        0        0   \n",
       "37657       0  ...        0        2        0        0        0        0   \n",
       "2638        0  ...        0        1        0        3        0        0   \n",
       "29046       0  ...        0        0        0        0        0        0   \n",
       "55121       0  ...        0        0        0        0        0        0   \n",
       "16629       1  ...        4        0        0        0        1        0   \n",
       "50562       0  ...        1        0        0        1        0        8   \n",
       "24556       0  ...        0        1        0        3        0        0   \n",
       "60774       1  ...        0        0        0        0        0        0   \n",
       "10056       0  ...        0        1        0        4        0        1   \n",
       "20861      13  ...        0        0        0        0        0        0   \n",
       "16188       0  ...        0       12        0       15        0        0   \n",
       "47867       0  ...        5        1        0        0        0        0   \n",
       "13832       0  ...        1        1        0        2        0        0   \n",
       "2623        0  ...        0        1        1        5        0        0   \n",
       "55871       0  ...        0        1        0        0        0        0   \n",
       "34959       0  ...        0        0        1        1        0        0   \n",
       "37206       0  ...        0        0        0        0        0        1   \n",
       "2375        0  ...        0        0        1        4        1        0   \n",
       "35490       0  ...        0        0        0        0        0        0   \n",
       "17761       1  ...        0        4        0        0        0        0   \n",
       "...       ...  ...      ...      ...      ...      ...      ...      ...   \n",
       "21039       0  ...        0        1        1        6        0        0   \n",
       "34539       0  ...        1        0        3        0        0        0   \n",
       "24041       3  ...        1        0        1        0        0        0   \n",
       "19910       0  ...        8        6        0        0        0        0   \n",
       "47220       1  ...        4        0        6        0        2        0   \n",
       "28765       0  ...        0        0        0        0        0        0   \n",
       "50125       0  ...        0        0        0        2        0        7   \n",
       "17812      22  ...        0        1        1        1        0        1   \n",
       "1251        0  ...        0        3        0        0        0        0   \n",
       "2899        0  ...        0        0        1        2        0        0   \n",
       "6456        0  ...        2        2        0        0        0        0   \n",
       "49653       0  ...        1        0        1        0        0        0   \n",
       "17695       1  ...        0        0        0        0        2        0   \n",
       "3890        0  ...        0        1        1        1        0        0   \n",
       "21318      27  ...        0        0        0        0        0        0   \n",
       "33220       0  ...        0        0        1        0        0        0   \n",
       "58102       0  ...        0        2        1        0        0        0   \n",
       "9941        0  ...        0        1        0        1        0        0   \n",
       "27692       0  ...        0        1        0        8        0        0   \n",
       "8895        0  ...        0        2        0        1        0        0   \n",
       "10411       0  ...        0        1        0        3        0        0   \n",
       "12022       0  ...        0        0        0        2        0        0   \n",
       "48848       0  ...        0        1        0        1        0       53   \n",
       "33441       0  ...        0        0        0        0        1        0   \n",
       "13956       0  ...        8        9        0        2        0        0   \n",
       "10272       4  ...        0        1        1        3        1        0   \n",
       "56601       4  ...        1        0        2        0        4        2   \n",
       "44426       0  ...        0        0        1        0        0        0   \n",
       "17440       0  ...        0        0        1        2        0        0   \n",
       "1942        0  ...        0        1        0        1        0        0   \n",
       "\n",
       "       feat_91  feat_92  feat_93   target  \n",
       "44641        0        1        1  Class_6  \n",
       "34885        0        0        0  Class_6  \n",
       "44806        0        0        0  Class_6  \n",
       "19016        1        0        0  Class_3  \n",
       "25875        0        0        0  Class_3  \n",
       "21530        0        0        0  Class_3  \n",
       "24732        0        0        0  Class_3  \n",
       "42111        0        0        0  Class_6  \n",
       "28761        0        0        0  Class_5  \n",
       "50497        0        0        0  Class_8  \n",
       "37657        0        2        0  Class_6  \n",
       "2638         0        0        0  Class_2  \n",
       "29046        0        1        0  Class_5  \n",
       "55121        0        0        0  Class_8  \n",
       "16629        0        0        0  Class_2  \n",
       "50562        0        1        0  Class_8  \n",
       "24556        0        0        0  Class_3  \n",
       "60774        0        0        0  Class_9  \n",
       "10056        0        0        0  Class_2  \n",
       "20861        0        0        0  Class_3  \n",
       "16188        0        0        0  Class_2  \n",
       "47867        0        0        0  Class_7  \n",
       "13832        0        0        0  Class_2  \n",
       "2623         0        1        0  Class_2  \n",
       "55871        0        0        1  Class_8  \n",
       "34959        0        1        0  Class_6  \n",
       "37206        0        0        0  Class_6  \n",
       "2375         0        0        0  Class_2  \n",
       "35490        0        0        0  Class_6  \n",
       "17761        0        0        0  Class_2  \n",
       "...        ...      ...      ...      ...  \n",
       "21039        0        0        0  Class_3  \n",
       "34539       24        0        0  Class_6  \n",
       "24041        0        0        0  Class_3  \n",
       "19910        0        0        0  Class_3  \n",
       "47220        0        0        2  Class_7  \n",
       "28765        0        0        0  Class_5  \n",
       "50125        0        0        0  Class_8  \n",
       "17812        0        0        0  Class_2  \n",
       "1251         0        0        0  Class_1  \n",
       "2899         0        0        0  Class_2  \n",
       "6456         0        0        0  Class_2  \n",
       "49653        0        0        0  Class_8  \n",
       "17695        0        0        0  Class_2  \n",
       "3890         0        1        0  Class_2  \n",
       "21318        0        0        0  Class_3  \n",
       "33220        0        5        0  Class_6  \n",
       "58102        0        0        0  Class_9  \n",
       "9941         0        0        0  Class_2  \n",
       "27692        0        0        0  Class_4  \n",
       "8895         0        0        0  Class_2  \n",
       "10411        0        0        0  Class_2  \n",
       "12022        0        0        0  Class_2  \n",
       "48848        0        0        0  Class_8  \n",
       "33441        0        1        0  Class_6  \n",
       "13956        0        0        0  Class_2  \n",
       "10272        0        3        0  Class_2  \n",
       "56601        0        0        0  Class_8  \n",
       "44426        1        1        0  Class_6  \n",
       "17440        0        0        0  Class_2  \n",
       "1942         0        0        0  Class_2  \n",
       "\n",
       "[10000 rows x 95 columns]>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取训练数据\n",
    "dpath = './data/'\n",
    "train = pd.read_csv(dpath +\"Otto_train.csv\")\n",
    "train.head()\n",
    "# dataFrame 随机抽取样本 https://blog.csdn.net/qq_22238533/article/details/71080942\n",
    "train = train.sample(n=10000,axis=0)\n",
    "train.shape\n",
    "train.head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "44641    Class_6\n",
      "34885    Class_6\n",
      "44806    Class_6\n",
      "19016    Class_3\n",
      "25875    Class_3\n",
      "21530    Class_3\n",
      "24732    Class_3\n",
      "42111    Class_6\n",
      "28761    Class_5\n",
      "50497    Class_8\n",
      "37657    Class_6\n",
      "2638     Class_2\n",
      "29046    Class_5\n",
      "55121    Class_8\n",
      "16629    Class_2\n",
      "50562    Class_8\n",
      "24556    Class_3\n",
      "60774    Class_9\n",
      "10056    Class_2\n",
      "20861    Class_3\n",
      "16188    Class_2\n",
      "47867    Class_7\n",
      "13832    Class_2\n",
      "2623     Class_2\n",
      "55871    Class_8\n",
      "34959    Class_6\n",
      "37206    Class_6\n",
      "2375     Class_2\n",
      "35490    Class_6\n",
      "17761    Class_2\n",
      "          ...   \n",
      "21039    Class_3\n",
      "34539    Class_6\n",
      "24041    Class_3\n",
      "19910    Class_3\n",
      "47220    Class_7\n",
      "28765    Class_5\n",
      "50125    Class_8\n",
      "17812    Class_2\n",
      "1251     Class_1\n",
      "2899     Class_2\n",
      "6456     Class_2\n",
      "49653    Class_8\n",
      "17695    Class_2\n",
      "3890     Class_2\n",
      "21318    Class_3\n",
      "33220    Class_6\n",
      "58102    Class_9\n",
      "9941     Class_2\n",
      "27692    Class_4\n",
      "8895     Class_2\n",
      "10411    Class_2\n",
      "12022    Class_2\n",
      "48848    Class_8\n",
      "33441    Class_6\n",
      "13956    Class_2\n",
      "10272    Class_2\n",
      "56601    Class_8\n",
      "44426    Class_6\n",
      "17440    Class_2\n",
      "1942     Class_2\n",
      "Name: target, Length: 10000, dtype: object\n",
      "type(X_train) <class 'pandas.core.frame.DataFrame'>\n",
      "(10000,)\n"
     ]
    }
   ],
   "source": [
    "y_train = train['target']   \n",
    "print(y_train)\n",
    "X_train = train.drop([\"id\", \"target\"], axis=1)\n",
    "print(\"type(X_train)\",type(X_train))\n",
    "# 原始特征\n",
    "train_id = train['id']\n",
    "print(train_id.shape)\n",
    "columns_org = X_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "pca = PCA(n_components = 0.85)\n",
    "pca.fit(X_train)\n",
    "    \n",
    "# 在训练集和测试集降维 \n",
    "X_train_pca = pca.transform(X_train)\n",
    "\n",
    "print(type(X_train_pca))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "34\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAD8CAYAAACfF6SlAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAEidJREFUeJzt3X+MXedd5/H3Z+0mLelu2iYWKkksm012kUtRtkwdkLqlatXgbLU1q3VKgoB0lZWphKWuWAQuSG7wUilB0OwfhB9eJd00WdaJAt0dbVxM1VRiF5VgJ6QJTjCdpqaxtzRukgZClQY33/3jHi9XlxnPmZnruXfmeb+kkc95znPufO+R/DnPfc49Z1JVSJLa8o8mXYAkafUZ/pLUIMNfkhpk+EtSgwx/SWqQ4S9JDTL8JalBhr8kNcjwl6QGbZx0AaMuvfTS2rJly6TLkKQ15ZFHHvl6VW3q23/qwn/Lli0cPXp00mVI0pqS5C+X0t9pH0lqkOEvSQ0y/CWpQYa/JDXI8JekBhn+ktQgw1+SGmT4S1KDeoV/kh1JjieZS7J3nu3vTPJokjNJdg21X53k80mOJXk8yY+Os3hJ0vIseodvkg3AHcB7gZPAkSSzVfXkULevAB8EfnZk928CP1lVX0zy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8Bn2CR47xew7/PIymmSpKLuotkJLkIuBb4s3PvNTWGH+9xE/A/J1hLL2cDtPNvmLJj3V3UuxN4qqo+PrRpao/1QjVP87FOsinJG7rl1zH4kshTDAJ1V9dt2o7zfDX/+dCgIAyuUZzzOK/Lb/sAdF8n+8/8/SMpJnWXcS9JvpvBaB8Gd17/zjTWnOS/A+9i8BTBrwEfBf4HcD+wGfhL4ANVNTUXWBeo+V0MpiEKOAH81NBc+sQleQfwv4EngFe75l9gMIc+lcf6HDXfyJQe6yTfx+CC7gYGg+H7q2p/9//xIIPpkz8FfrwbUU/cOWp+CNgEBHgM+NDQheF/+DrrNfwlSQtbr9M+kqRzMPwlqUGGvyQ1yPCXpAYZ/pLUIMNfkhpk+EtSgwx/SWrQ/wPI1t4ORx0GrwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "print(type(pca.explained_variance_ratio_))\n",
    "print(pca.explained_variance_ratio_.size)\n",
    "# pca 类两个值得关注的成员：\n",
    "# 1.explained_variance_，它代表降维后的各主成分的方差值。方差值越大，则说明越是重要的主成分。\n",
    "# 2.explained_variance_ratio_，它代表降维后的各主成分的方差值占总方差值的比例，这个比例越大，则越是重要的主成分。\n",
    "plt.bar(range(len(pca.explained_variance_ratio_)), pca.explained_variance_ratio_)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.int64'>\n",
      "<class 'pandas.core.series.Series'>\n",
      "<class 'pandas.core.series.Series'>\n",
      "(10000,)\n",
      "train_id  <class 'pandas.core.series.Series'>\n",
      "(10000,)\n",
      "(10000, 34)\n",
      "train_pca.shape (10000, 36)\n"
     ]
    }
   ],
   "source": [
    "# 保存pca变换结果\n",
    "n_components = pca.n_components_\n",
    "print (type(n_components))\n",
    "\n",
    "feat_names_pca = []\n",
    "for i in range(n_components):\n",
    "    feat_names_pca.append(\"pca_\" + str(i))\n",
    "\n",
    "print(type(y_train))\n",
    "y = pd.Series(data = y_train, name = 'target')\n",
    "print(type(y))\n",
    "print(y.shape)\n",
    "print(\"train_id \",type(train_id))\n",
    "print(train_id.shape)\n",
    "print(X_train_pca.shape)\n",
    "#  X_train 经过 pca 模型转换后得到 X_train_pca 已经失去索引，而 pd.DataFrame 不设置索引默认是从 0 开始的\n",
    "train_pca = pd.concat([train_id, pd.DataFrame(index=train_id.index, columns = feat_names_pca, data = X_train_pca), y], axis = 1)\n",
    "#train_pca = pd.concat([train_id, pd.DataFrame(columns = feat_names_pca, data = X_train_pca), y], axis = 1)\n",
    "print(\"train_pca.shape\",train_pca.shape)\n",
    "train_pca.to_csv(dpath +'Otto_FE_train_PCA_random10000.csv',index=False,header=True)\n",
    "#print(train_pca[train_pca.isnull().T.any()].shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>pca_0</th>\n",
       "      <th>pca_1</th>\n",
       "      <th>pca_2</th>\n",
       "      <th>pca_3</th>\n",
       "      <th>pca_4</th>\n",
       "      <th>pca_5</th>\n",
       "      <th>pca_6</th>\n",
       "      <th>pca_7</th>\n",
       "      <th>pca_8</th>\n",
       "      <th>...</th>\n",
       "      <th>pca_25</th>\n",
       "      <th>pca_26</th>\n",
       "      <th>pca_27</th>\n",
       "      <th>pca_28</th>\n",
       "      <th>pca_29</th>\n",
       "      <th>pca_30</th>\n",
       "      <th>pca_31</th>\n",
       "      <th>pca_32</th>\n",
       "      <th>pca_33</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>44642</td>\n",
       "      <td>-2.401485</td>\n",
       "      <td>4.392327</td>\n",
       "      <td>0.520809</td>\n",
       "      <td>-2.201817</td>\n",
       "      <td>-1.233103</td>\n",
       "      <td>-0.482438</td>\n",
       "      <td>0.371232</td>\n",
       "      <td>2.679550</td>\n",
       "      <td>-0.693654</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.725258</td>\n",
       "      <td>0.215070</td>\n",
       "      <td>0.259147</td>\n",
       "      <td>0.616864</td>\n",
       "      <td>0.221898</td>\n",
       "      <td>0.028583</td>\n",
       "      <td>0.618058</td>\n",
       "      <td>0.730880</td>\n",
       "      <td>0.236966</td>\n",
       "      <td>Class_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34886</td>\n",
       "      <td>-1.613945</td>\n",
       "      <td>6.905888</td>\n",
       "      <td>-0.684274</td>\n",
       "      <td>-6.444908</td>\n",
       "      <td>-3.673110</td>\n",
       "      <td>7.966786</td>\n",
       "      <td>29.666846</td>\n",
       "      <td>19.479659</td>\n",
       "      <td>11.027421</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.307331</td>\n",
       "      <td>-0.631211</td>\n",
       "      <td>-0.869630</td>\n",
       "      <td>0.638846</td>\n",
       "      <td>0.124559</td>\n",
       "      <td>1.460256</td>\n",
       "      <td>1.675033</td>\n",
       "      <td>0.120857</td>\n",
       "      <td>0.192366</td>\n",
       "      <td>Class_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>44807</td>\n",
       "      <td>-2.917818</td>\n",
       "      <td>13.187745</td>\n",
       "      <td>-0.953303</td>\n",
       "      <td>-3.309427</td>\n",
       "      <td>-0.126315</td>\n",
       "      <td>0.449860</td>\n",
       "      <td>-1.212859</td>\n",
       "      <td>-0.921445</td>\n",
       "      <td>-0.654326</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.023734</td>\n",
       "      <td>0.175676</td>\n",
       "      <td>-0.922382</td>\n",
       "      <td>0.176985</td>\n",
       "      <td>-0.817531</td>\n",
       "      <td>0.840582</td>\n",
       "      <td>0.059230</td>\n",
       "      <td>3.272305</td>\n",
       "      <td>-1.957479</td>\n",
       "      <td>Class_6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>19017</td>\n",
       "      <td>-4.250615</td>\n",
       "      <td>-7.726496</td>\n",
       "      <td>0.474893</td>\n",
       "      <td>9.366311</td>\n",
       "      <td>-1.566268</td>\n",
       "      <td>13.076900</td>\n",
       "      <td>2.782658</td>\n",
       "      <td>-6.785141</td>\n",
       "      <td>0.338254</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.372314</td>\n",
       "      <td>3.791879</td>\n",
       "      <td>2.771023</td>\n",
       "      <td>-1.568888</td>\n",
       "      <td>2.397054</td>\n",
       "      <td>-0.444911</td>\n",
       "      <td>-0.000162</td>\n",
       "      <td>1.950526</td>\n",
       "      <td>-1.835864</td>\n",
       "      <td>Class_3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>25876</td>\n",
       "      <td>-1.112903</td>\n",
       "      <td>-0.338095</td>\n",
       "      <td>0.491536</td>\n",
       "      <td>-0.069408</td>\n",
       "      <td>-1.208544</td>\n",
       "      <td>-2.191797</td>\n",
       "      <td>-1.129590</td>\n",
       "      <td>2.516341</td>\n",
       "      <td>-1.490161</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.239775</td>\n",
       "      <td>-0.920964</td>\n",
       "      <td>0.184961</td>\n",
       "      <td>-1.144437</td>\n",
       "      <td>0.133968</td>\n",
       "      <td>-1.236665</td>\n",
       "      <td>-1.801884</td>\n",
       "      <td>-0.609749</td>\n",
       "      <td>-1.325505</td>\n",
       "      <td>Class_3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      id     pca_0      pca_1     pca_2     pca_3     pca_4      pca_5  \\\n",
       "0  44642 -2.401485   4.392327  0.520809 -2.201817 -1.233103  -0.482438   \n",
       "1  34886 -1.613945   6.905888 -0.684274 -6.444908 -3.673110   7.966786   \n",
       "2  44807 -2.917818  13.187745 -0.953303 -3.309427 -0.126315   0.449860   \n",
       "3  19017 -4.250615  -7.726496  0.474893  9.366311 -1.566268  13.076900   \n",
       "4  25876 -1.112903  -0.338095  0.491536 -0.069408 -1.208544  -2.191797   \n",
       "\n",
       "       pca_6      pca_7      pca_8  ...    pca_25    pca_26    pca_27  \\\n",
       "0   0.371232   2.679550  -0.693654  ... -0.725258  0.215070  0.259147   \n",
       "1  29.666846  19.479659  11.027421  ... -0.307331 -0.631211 -0.869630   \n",
       "2  -1.212859  -0.921445  -0.654326  ... -0.023734  0.175676 -0.922382   \n",
       "3   2.782658  -6.785141   0.338254  ... -0.372314  3.791879  2.771023   \n",
       "4  -1.129590   2.516341  -1.490161  ... -0.239775 -0.920964  0.184961   \n",
       "\n",
       "     pca_28    pca_29    pca_30    pca_31    pca_32    pca_33   target  \n",
       "0  0.616864  0.221898  0.028583  0.618058  0.730880  0.236966  Class_6  \n",
       "1  0.638846  0.124559  1.460256  1.675033  0.120857  0.192366  Class_6  \n",
       "2  0.176985 -0.817531  0.840582  0.059230  3.272305 -1.957479  Class_6  \n",
       "3 -1.568888  2.397054 -0.444911 -0.000162  1.950526 -1.835864  Class_3  \n",
       "4 -1.144437  0.133968 -1.236665 -1.801884 -0.609749 -1.325505  Class_3  \n",
       "\n",
       "[5 rows x 36 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "train = pd.read_csv(dpath +\"Otto_FE_train_PCA_random10000.csv\")\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
